<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-row>
              <el-button
                icon="el-icon-plus"
                type="primary"
                size="small"
                @click="isdialog = true"
                >新增角色</el-button
              >
            </el-row>
            <el-table border style="width: 100%" :data="list">
              <el-table-column type="index" label="序号" width="120">
              </el-table-column>
              <el-table-column prop="name" label="角色名" width="120">
              </el-table-column>
              <el-table-column prop="description" label="描述">
              </el-table-column>
              <el-table-column prop="czo" label="操作" width="180">
                <template slot-scope="{ row }">
                  <el-button size="small" type="text" @click="fpClick(row.id)"
                    >分配权限</el-button
                  >
                  <el-button size="small" type="text" @click="xgClick(row.id)"
                    >修改</el-button
                  >
                  <el-button size="small" type="text" @click="delClick(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              style="margin-top: 20px"
              @current-change="changePage"
              :current-page="page.page"
              :page-size="page.pagesize"
              layout="total, prev, pager, next"
              :total="page.total"
            >
            </el-pagination>
            <el-dialog :title="title" :visible="isdialog" @close="guanbi">
              <el-form
                :model="form"
                ref="forms"
                :rules="rules"
                label-width="120px"
              >
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                  <el-input v-model="form.description"></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="guanbi">取 消</el-button>
                <el-button type="primary" @click="OKClick">确 定</el-button>
              </div>
            </el-dialog>
            <el-dialog title="分配权限" :visible="showDialog" @close="isQX">
              <el-tree
                :data="qxlist"
                ref="permTree"
                :props="defaultProps"
                default-expand-all
                style="text-align: center"
                show-checkbox
                check-strictly
                check-on-click-node
                node-key="id"
                :default-checked-keys="selectCheck"
              />
              <el-row type="flex">
                <el-col style="text-align: center">
                  <el-button @click="isQX">取消</el-button>
                  <el-button type="primary" @click="isquanxian">确定</el-button>
                </el-col>
              </el-row>
            </el-dialog>
          </el-tab-pane>
          <el-tab-pane label="公司设置" name="second">
            <el-form
              style="width: 450px; margin-left: 100px"
              label-width="80px"
              :model="formData"
            >
              <el-form-item label="企业名称">
                <el-input disabled v-model="formData.name"></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input disabled v-model="formData.companyAddress"></el-input>
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input disabled v-model="formData.companyPhone"></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled v-model="formData.mailbox"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input disabled v-model="formData.remarks"></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  setAssignprem,
  updateRole,
  getRoleDetail,
  addRole,
} from "@/api/setting.js";
import { getPermisson } from "@/api/permisson.js";
import { tranListToTreeData } from "@/utils";
export default {
  name: "setting",
  data() {
    return {
      activeName: "first",
      formData: {},
      list: [],
      selectCheck: [],
      roleId: null,
      showDialog: false,
      isdialog: false,
      form: {},
      qxlist: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      page: {
        page: 1,
        pagesize: 10,
        total: 0,
      },
      rules: {
        name: [{ required: true, message: "角色名不能为空", trigger: "blur" }],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage; // 将当前页码赋值给当前的最新页码
      this.getRoleList();
    },
    // 获取角色
    async getRoleList() {
      let res = await getRoleList(this.page);
      this.page.total = res.total;
      this.list = res.rows;
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.$store.getters.companyId);
    },
    // 删除角色
    async delClick(id) {
      try {
        await this.$confirm("确定删除该角色吗?", "温馨提示");
        await deleteRole(id);
        if (this.list.length == 1) {
          this.page.page--;
        }
        this.getRoleList();

        this.$message.success("角色删除成功");
      } catch (error) {}
    },
    // 修改角色，获取信息
    async xgClick(id) {
      this.form = await getRoleDetail(id);
      this.isdialog = true;
    },
    // 分配权限获取
    async fpClick(id) {
      this.qxlist = tranListToTreeData(await getPermisson(), "0");
      const { permIds } = await getRoleDetail(id);
      this.roleId = id;
      this.selectCheck = permIds;
      this.showDialog = true;
    },
    // 分配权限确定
    async isquanxian() {
      let res = this.$refs.permTree.getCheckedKeys();
      await setAssignprem({ permIds: res, id: this.roleId });
      this.$message.success("分配成功");
      this.showDialog = false;
    },
    // 分配权限取消
    isQX() {
      this.roleId = null;
      this.selectCheck = [];
      this.showDialog = false;
    },
    // 确定修改
    async OKClick() {
      try {
        // 验证通过之后才修改
        await this.$refs.forms.validate();
        if (this.form.id) {
          await updateRole(this.form);
        } else {
          // 新增操作
          await addRole(this.form);
          this.page.total++;
          this.page.page = Math.ceil(this.page.total / this.page.pagesize);
        }
        // 更新数据
        this.getRoleList();
        this.$message.success("操作成功");
        // 关闭弹窗
        this.isdialog = false;
      } catch (error) {
        console.log(error);
      }
    },
    guanbi() {
      this.form = {};
      this.$refs.forms.resetFields();
      this.isdialog = false;
    },
  },
  computed: {
    title() {
      return this.form.id ? "编辑角色" : "新增角色";
    },
  },
  created() {
    this.getRoleList(this.page);
    this.getCompanyInfo();
  },
};
</script>

<style>
.el-tabs__nav-scroll .el-tabs__item {
  font-size: 20px;
  padding: 0 20px;
}
.el-tabs__content {
  padding: 40px 20px;
}
.el-table {
  margin-top: 20px;
}
.el-pagination {
  text-align: right;
}
</style>
